import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../locator_service.dart';
import '../controller/index.dart';
import '../widgets/home_widget.dart';
import '../widgets/initial_widget.dart';
import '../../../../core/widgets/index.dart' as core_widgets;

class HomeView extends StatelessWidget {
  const HomeView({Key? key}) : super(key: key);

  Widget _builBody(BuildContext context) {
    return BlocBuilder<HomeCubit, HomeState>(builder: (context, state) {
      return state.when(
        initial: () => InitialWidget(
          init: () => BlocProvider.of<HomeCubit>(context).init(),
        ),
        home:() => HomeWidget(
          openFact: () => BlocProvider.of<HomeCubit>(context).openFact(),
          openTasks: () => BlocProvider.of<HomeCubit>(context).openTasks(),
          openReports: () => BlocProvider.of<HomeCubit>(context).openReports(),
          openSettings: () => BlocProvider.of<HomeCubit>(context).openSettings(),
        ),
        error: (failure) => core_widgets.ErrorWidget(message: failure.message),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<HomeCubit>(
      create: (_) => sl<HomeCubit>()..init(),
      child: _builBody(context),
    );
  }
}
